#include "realtimeform.h"
#include "ui_realtimeform.h"

#include "globalvars.h"
#include <QStringBuilder>

RealTimeForm::RealTimeForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RealTimeForm)
{
    ui->setupUi(this);

    m_tempTimer = new QTimer(this);
    m_humiTimer = new QTimer(this);
    m_tipsTimer = new QTimer(this);
    connect(m_tempTimer, SIGNAL(timeout()),
            this, SLOT(slotTimeOutTemp()));
    connect(m_humiTimer, SIGNAL(timeout()),
            this, SLOT(slotTimeOutHumi()));
    connect(m_tipsTimer, SIGNAL(timeout()),
            this, SLOT(slotTimeOutTips()));
}

RealTimeForm::~RealTimeForm()
{
    delete ui;
}

void RealTimeForm::slotTimeOutTemp(void)
{
    ///定时获取温度数据
    QString msg = QString(CMD_DevTemp_T)
            % QString("#") % GlobalVars::g_localUsrInfo.getID();
    emit signalGainTemp(msg);
}

void RealTimeForm::slotTimeOutHumi(void)
{
    ///定时获取温度数据
    QString msg = QString(CMD_DevHumi_H)
            % QString("#") % GlobalVars::g_localUsrInfo.getID();
    emit signalGainTemp(msg);
}

void RealTimeForm::slotTimeOutTips(void)
{
    ///定时获取温度数据
    QString msg = QString(CMD_DevMode_M)
            % QString("#") % GlobalVars::g_localUsrInfo.getID();
    emit signalGainTemp(msg);
}

void RealTimeForm::on_pb_startTempTIM_clicked()
{
    if(m_tempTimer->isActive())
    {
        m_tempTimer->stop();
        ui->pb_startTempTIM->setText("开启");
    }else
    {
        m_tempTimer->start(ui->sb_tempInterval->value()*1000);
        ui->pb_startTempTIM->setText("停止");
    }
}

void RealTimeForm::on_pb_startHumiTIM_clicked()
{
    if(m_humiTimer->isActive())
    {
        m_humiTimer->stop();
        ui->pb_startHumiTIM->setText("开启");
    }else
    {
        m_humiTimer->start(ui->sb_humiInterval->value()*1000);
        ui->pb_startHumiTIM->setText("停止");
    }
}

void RealTimeForm::on_pb_startTipsTIM_clicked()
{
    if(m_tipsTimer->isActive())
    {
        m_tipsTimer->stop();
        ui->pb_startTipsTIM->setText("开启");
    }else
    {
        m_tipsTimer->start(ui->sb_tipsInterval->value()*1000);
        ui->pb_startTipsTIM->setText("停止");
    }
}

void RealTimeForm::slotparseDevTemp(QString data)
{
    QStringList list = data.split("|");
    ui->lcd_tempV->display(list.at(0));
    ui->lcd_tempMaxV->display(list.at(1));
    ui->lcd_tempMinV->display(list.at(2));
}

void RealTimeForm::slotparseDevHumi(QString data)
{
    QStringList list = data.split("|");
    ui->lcd_humiV->display(list.at(0));
    ui->lcd_humiMaxV->display(list.at(1));
    ui->lcd_humiMinV->display(list.at(2));
}

void RealTimeForm::slotparseDevMode(QString data)
{
    QStringList list = data.split("|");
    ui->lb_mode->setText(QString("模式: ") + list.at(0));
    ui->lb_warn->setText(QString("警告: ") + list.at(1));
}
